package com.yaodian100.ec.member.entity;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

import com.yaodian100.core.common.entity.BaseEntity;
import com.yaodian100.core.common.utils.ServerValue;

/**
 * @author feeling.wang
 * 
 */
@Entity
@Table(name = "EC_MEMBER_MAIN")
public class Member extends BaseEntity {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 291163216425620006L;;

	@Id
	@GeneratedValue(generator = "seq_ec_member_main")
	@GenericGenerator(name = "seq_ec_member_main", strategy = "com.yaodian100.core.common.dao.hibernate.SeqStringGenerator", parameters = {
			@Parameter(name = "sequence", value = "SEQ_EC_MEMBER_MAIN"), @Parameter(name = "pattern", value = "0000000000") })
	@Column(name = "MEMBER_ID", length = 10, nullable = false)
	private String memberId;

	@Column(name = "LOGIN_ID", length = 100, nullable = false)
	private String loginId;

	@Column(name = "PWD", length = 100, nullable = false)
	private String pwd;

	@Column(name = "NAME", length = 100)
	private String name;

	@Column(name = "SEX", length = 1)
	private String sex;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "BIRTHDAY")
	private Date birthday;

	@Column(name = "EMAIL", length = 100)
	private String email;

	@Column(name = "HOME_PHONE", length = 20)
	private String homePhone;

	@Column(name = "COMPANY_PHONE", length = 20)
	private String companyPhone;

	@Column(name = "MOBILE", length = 20)
	private String mobile;

	@Column(name = "zipCode", length = 10)
	private String zipCode;

	@Column(name = "province", length = 20)
	private String province;

	@Column(name = "city", length = 20)
	private String city;

	@Column(name = "district", length = 20)
	private String district;

	@Column(name = "ADDR", length = 100)
	private String addr;

	@Column(name = "FG_PWD_COUNT")
	private int fgPwdCount = 0;

	@Column(name = "STATUS", length = 2, nullable = false)
	private String status;

	@Column(name = "RANK", length = 1, nullable = false)
	private String rank;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "ACTIVE_DATE")
	private Date activeDate;

	@Column(name = "APPLY_SOURCE", length = 100)
	private String applySource;

	@Column(name = "REFERRAL")
	private int referral;

	@Column(name = "CLIENT_IP", length = 100, nullable = false)
	private String clientIp;

	@Column(name = "EDM", length = 1)
	private String edm = "Y";

	@Column(name = "SMS", length = 1)
	private String sms = "Y";

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "UNBLOCK_DATE")
	private Date unBlockDate;

	@Column(name = "BLACK_LIST_ID")
	private int blackListId = 0;

	@OneToOne(cascade = CascadeType.ALL)
	@PrimaryKeyJoinColumn
	private Balance balance;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "LAST_LOGIN_TIME")
	private Date lastLoginTime;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "ACTIVATE_EXPIRE_DATE")
	private Date activateExpireDate;

	@Column(name = "TEMP_EMAIL", length = 100)
	private String tempEmail;

	@Column(name = "ACTIVATE_TOKEN", length = 20)
	private String activateToken;

	// ===== new for 手机激活 =====
	// 会员注册之激活累计次数
	@Column(name = "ACTIVATE_COUNT")
	private int activateCount = 0;

	// 记录修改手机号之暂存栏位
	@Column(name = "TEMP_MOBILE", length = 11)
	private String tempMobile;

	// 最近一次修改手机号之日期时间
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "LAST_MOBILE_MODIFY_DATE")
	private Date lastMobileModifyDate;

	// 会员注册之激活累计次数
	@Column(name = "MOBILE_MODIFY_COUNT")
	private int mobileModifyCount = 0;

	// 最近一次手机忘记密码之日期时间
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "LAST_MOBILE_FORGET_PWD_DATE")
	private Date lastMobileForgetPwdDate;

	// 会员忘记密码累计次数
	@Column(name = "MOBILE_FORGET_PWD_COUNT")
	private int mobileForgetPwdCount = 0;

	// 手机验证码
	@Column(name = "ACTIVATE_MOBILE_TOKEN", length = 8)
	private String activateMobileToken;

	// 邮箱找回密码验证码
	@Column(name = "FORGET_PWD_TOKEN", length = 13)
	private String forgetPwdToken;

	@Column(name = "DOMAIN_SRC", length = 30)
	private String domainSrc;

	/**
	 * 渠道编号
	 */
	@Column(name = "CHANNEL_ID")
	private Long channelId;

	@Column(name = "CHANNEL_CHILD", length = 500)
	private String channelChild;

	/**
	 * 登入次数累计
	 */
	@Column(name = "LOGIN_COUNT")
	private int loginCount = 0;

	/**
	 * 會員驗證狀態 1:已看過訊息 2:已更新性别生日 3:已獲得積分
	 */
	@Column(name = "CRM_UPDATE", length = 1)
	private String crmUpdate;

	public Member() {
		super();
	}

	/**
	 * 取得发信用email
	 * 
	 * @return
	 */
	@Transient
	public String getSendEmail() {
		if (StringUtils.isBlank(email)) {
			return tempEmail;
		}
		return email;
	}

	/**
	 * 是否為信任登入會員
	 * 
	 * @return
	 */
	@Transient
	public boolean getIsTrustMember() {
		for (String as : ServerValue.getTrustMembers()) {
			if (getApplySource().equals(as)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 是否为手机会员
	 * 
	 * @return
	 */
	@Transient
	public boolean getIsMobileMember() {
		return StringUtils.isNotBlank(mobile);
	}

	@Transient
	private int getBonus = 0;

	public int getGetBonus() {
		return getBonus;
	}

	public void setGetBonus(int getBonus) {
		this.getBonus = getBonus;
	}

	/**
	 * @return the activeDate
	 */
	public Date getActiveDate() {
		return activeDate;
	}

	/**
	 * @return the addr
	 */
	public String getAddr() {
		return addr;
	}

	/**
	 * @return the applySource
	 */
	public String getApplySource() {
		return applySource;
	}

	public Balance getBalance() {
		return balance;
	}

	/**
	 * @return the birthday
	 */
	public Date getBirthday() {
		return birthday;
	}

	/**
	 * @return the blackListId
	 */
	public int getBlackListId() {
		return blackListId;
	}

	/**
	 * @return the city
	 */
	public String getCity() {
		return city;
	}

	/**
	 * @return the clientIp
	 */
	public String getClientIp() {
		return clientIp;
	}

	/**
	 * @return the companyPhone
	 */
	public String getCompanyPhone() {
		return companyPhone;
	}

	/**
	 * @return the district
	 */
	public String getDistrict() {
		return district;
	}

	/**
	 * @return the edm
	 */
	public String getEdm() {
		return edm;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @return the fgPwdCount
	 */
	public int getFgPwdCount() {
		return fgPwdCount;
	}

	/**
	 * @return the homePhone
	 */
	public String getHomePhone() {
		return homePhone;
	}

	public Date getLastLoginTime() {
		return lastLoginTime;
	}

	/**
	 * @return the loginId
	 */
	public String getLoginId() {
		return loginId;
	}

	public String getMemberId() {
		return memberId;
	}

	/**
	 * @return the mobile
	 */
	public String getMobile() {
		return mobile;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the province
	 */
	public String getProvince() {
		return province;
	}

	/**
	 * @return the pwd
	 */
	public String getPwd() {
		return pwd;
	}

	/**
	 * @return the rank
	 */
	public String getRank() {
		return rank;
	}

	/**
	 * @return the referral
	 */
	public int getReferral() {
		return referral;
	}

	/**
	 * @return the sex
	 */
	public String getSex() {
		return sex;
	}

	/**
	 * @return the status
	 */
	public String getStatus() {
		return status;
	}

	/**
	 * @return the unBlockDate
	 */
	public Date getUnBlockDate() {
		return unBlockDate;
	}

	/**
	 * @return the zipCode
	 */
	public String getZipCode() {
		return zipCode;
	}

	/**
	 * @param activeDate the activeDate to set
	 */
	public void setActiveDate(Date activeDate) {
		this.activeDate = activeDate;
	}

	/**
	 * @param addr the addr to set
	 */
	public void setAddr(String addr) {
		this.addr = addr;
	}

	/**
	 * @param applySource the applySource to set
	 */
	public void setApplySource(String applySource) {
		this.applySource = applySource;
	}

	public void setBalance(Balance balance) {
		this.balance = balance;
	}

	/**
	 * @param birthday the birthday to set
	 */
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	/**
	 * @param blackListId the blackListId to set
	 */
	public void setBlackListId(int blackListId) {
		this.blackListId = blackListId;
	}

	/**
	 * @param city the city to set
	 */
	public void setCity(String city) {
		this.city = city;
	}

	/**
	 * @param clientIp the clientIp to set
	 */
	public void setClientIp(String clientIp) {
		this.clientIp = clientIp;
	}

	/**
	 * @param companyPhone the companyPhone to set
	 */
	public void setCompanyPhone(String companyPhone) {
		this.companyPhone = companyPhone;
	}

	/**
	 * @param district the district to set
	 */
	public void setDistrict(String district) {
		this.district = district;
	}

	/**
	 * @param edm the edm to set
	 */
	public void setEdm(String edm) {
		this.edm = edm;
	}

	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @param fgPwdCount the fgPwdCount to set
	 */
	public void setFgPwdCount(int fgPwdCount) {
		this.fgPwdCount = fgPwdCount;
	}

	/**
	 * @param homePhone the homePhone to set
	 */
	public void setHomePhone(String homePhone) {
		this.homePhone = homePhone;
	}

	public void setLastLoginTime(Date lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	/**
	 * @param loginId the loginId to set
	 */
	public void setLoginId(String loginId) {
		this.loginId = loginId;
	}

	public void setMemberId(String memberId) {
		this.memberId = memberId;
	}

	/**
	 * @param mobile the mobile to set
	 */
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @param province the province to set
	 */
	public void setProvince(String province) {
		this.province = province;
	}

	/**
	 * @param pwd the pwd to set
	 */
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	/**
	 * @param rank the rank to set
	 */
	public void setRank(String rank) {
		this.rank = rank;
	}

	/**
	 * @param referral the referral to set
	 */
	public void setReferral(int referral) {
		this.referral = referral;
	}

	/**
	 * @param sex the sex to set
	 */
	public void setSex(String sex) {
		this.sex = sex;
	}

	/**
	 * @param status the status to set
	 */
	public void setStatus(String status) {
		this.status = status;
	}

	/**
	 * @param unBlockDate the unBlockDate to set
	 */
	public void setUnBlockDate(Date unBlockDate) {
		this.unBlockDate = unBlockDate;
	}

	/**
	 * @param zipCode the zipCode to set
	 */
	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}

	public Date getActivateExpireDate() {
		return activateExpireDate;
	}

	public void setActivateExpireDate(Date activateExpireDate) {
		this.activateExpireDate = activateExpireDate;
	}

	public String getTempEmail() {
		return tempEmail;
	}

	public void setTempEmail(String tempEmail) {
		this.tempEmail = tempEmail;
	}

	public String getActivateToken() {
		return activateToken;
	}

	public void setActivateToken(String activateToken) {
		this.activateToken = activateToken;
	}

	/**
	 * @return the activateCount
	 */
	public int getActivateCount() {
		return activateCount;
	}

	/**
	 * @param activateCount the activateCount to set
	 */
	public void setActivateCount(int activateCount) {
		this.activateCount = activateCount;
	}

	/**
	 * @return the tempMobile
	 */
	public String getTempMobile() {
		return tempMobile;
	}

	/**
	 * @param tempMobile the tempMobile to set
	 */
	public void setTempMobile(String tempMobile) {
		this.tempMobile = tempMobile;
	}

	/**
	 * @return the lastMobileModifyDate
	 */
	public Date getLastMobileModifyDate() {
		return lastMobileModifyDate;
	}

	/**
	 * @param lastMobileModifyDate the lastMobileModifyDate to set
	 */
	public void setLastMobileModifyDate(Date lastMobileModifyDate) {
		this.lastMobileModifyDate = lastMobileModifyDate;
	}

	/**
	 * @return the mobileModifyCount
	 */
	public int getMobileModifyCount() {
		return mobileModifyCount;
	}

	/**
	 * @param mobileModifyCount the mobileModifyCount to set
	 */
	public void setMobileModifyCount(int mobileModifyCount) {
		this.mobileModifyCount = mobileModifyCount;
	}

	/**
	 * @return the lastMobileForgetPwdDate
	 */
	public Date getLastMobileForgetPwdDate() {
		return lastMobileForgetPwdDate;
	}

	/**
	 * @param lastMobileForgetPwdDate the lastMobileForgetPwdDate to set
	 */
	public void setLastMobileForgetPwdDate(Date lastMobileForgetPwdDate) {
		this.lastMobileForgetPwdDate = lastMobileForgetPwdDate;
	}

	/**
	 * @return the mobileForgetPwdCount
	 */
	public int getMobileForgetPwdCount() {
		return mobileForgetPwdCount;
	}

	/**
	 * @param mobileForgetPwdCount the mobileForgetPwdCount to set
	 */
	public void setMobileForgetPwdCount(int mobileForgetPwdCount) {
		this.mobileForgetPwdCount = mobileForgetPwdCount;
	}

	/**
	 * @return the activateMobileToken
	 */
	public String getActivateMobileToken() {
		return activateMobileToken;
	}

	/**
	 * @param activateMobileToken the activateMobileToken to set
	 */
	public void setActivateMobileToken(String activateMobileToken) {
		this.activateMobileToken = activateMobileToken;
	}

	public String getForgetPwdToken() {
		return forgetPwdToken;
	}

	public void setForgetPwdToken(String forgetPwdToken) {
		this.forgetPwdToken = forgetPwdToken;
	}

	public String getDomainSrc() {
		return domainSrc;
	}

	public void setDomainSrc(String domainSrc) {
		this.domainSrc = domainSrc;
	}

	public Long getChannelId() {
		return channelId;
	}

	public void setChannelId(Long channelId) {
		this.channelId = channelId;
	}

	public String getChannelChild() {
		return channelChild;
	}

	public void setChannelChild(String channelChild) {
		this.channelChild = channelChild;
	}

	public int getLoginCount() {
		return loginCount;
	}

	public void setLoginCount(int loginCount) {
		this.loginCount = loginCount;
	}

	public String getSms() {
		return sms;
	}

	public void setSms(String sms) {
		this.sms = sms;
	}

	public String getCrmUpdate() {
		return crmUpdate;
	}

	public void setCrmUpdate(String crmUpdate) {
		this.crmUpdate = crmUpdate;
	}
}